<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class show_style_controller extends admin_controller
{
    // Get list
    public function __construct()
    {
        parent::__construct();
        if (!$this->is_admin_logged()) redirect(base_url() . $this->base_admin_url . 'login_controller');
        $this->page_title = 'Show Style';
        $this->table_name = 'show_style';
        $this->route_name = 'show_style_controller';
        $this->business_model = $this->show_style_model;
        $this->insert_default_code();
    }

    private function insert_default_code()
    {
        $show_model = $this->show_style_model;
        if(show_style_model::get_element_by_code(show_style_model::$CODE)->code == '')
        {
            $show_model->code = show_style_model::$CODE;
            $show_model->insert_data();
            redirect(base_url() . $this->base_admin_url . 'show_style_controller/update/'. show_style_model::get_element_by_code(show_style_model::$CODE)->id);
        }
    }

    public function hook_redirect_url_after_save($id)
    {
        return base_url() . $this->base_admin_url . $this->route_name . '/update/'.$id;
    }

    public function get_list()
    {
        $list = array();
        $news = $this->business_model->get_elements();
        //        echo json_encode($news);exit;
        foreach ($news as $item)
        {
            $actions = array();
            $actions[] = array(
                'text' => 'Sửa',
                'href' => base_url() . $this->base_admin_url . $this->route_name . '/update/' . $item->id
            );

            $list[] = array(
                'can_delete' => $item->id,
                'id' => $item->id,
                'style' => $item->style,
                'actions' => $actions
            );
        }
        return $list;
    }

    public function get_header_list()
    {
        $list = array();
        $list[] = 'ID';
        $list[] = 'Kiểu hiển thị';
        $list[] = 'Hành động';
        return $list;

    }

    public function get_list_actions()
    {
        $actions = array();
        $actions[] = array(
            'text' => 'Thêm',
            'href' => base_url() . $this->base_admin_url . $this->route_name . '/update/-1',
            'view_change_method' => 'redirect'
        );
        $actions[] = array(
            'text' => 'Xóa',
            'href' => base_url() . $this->base_admin_url . $this->route_name . '/delete',
            'view_change_method' => 'submit'
        );
        return $actions;
    }

    public function get_post_data()
    {
        $this->business_model->code = show_style_model::$CODE;
        $this->business_model->style = $this->input->post('style');
    }

    public function get_fields($id)
    {
        if ($id == -1)
            $model = new $this->business_model();
        else
            $model = $this->business_model->get_element_by_id($id);

        $fields = array();

        $fields[] = array(
            'text' => 'Kiểu hiển thị',
            'name' => 'style',
            'value' => $model->style,
            'type' => 'select',
            'options' => array(
                array_to_object(array(
                    'text' => 'SLIDE SHOW',
                    'value' => show_style_model::$STYLE_SLIDE
                )),
                array_to_object(array(
                    'text' => 'NEW SHOW',
                    'value' => show_style_model::$STYLE_NEW
                ))
            ),
            'select_display' => 'text',
            'select_value' => 'value'
        );

        return $fields;
    }

}
